﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPlot;

namespace FSToolkit
{
    public partial class ScatterChartcs : UserControl
    {
        private NPlot.Windows.PlotSurface2D plotSurface;
        private readonly  PreprocessData preData;
        public Color[] StandardColor { get; set; } = new Color[6] { Color.Green, Color.Purple, Color.Gold, Color.Red, Color.Brown, Color.PowderBlue };
        public string ChartTitle { get; private set; } = "ScatterChart";
        public string XTitle { get; private set; } = "AbscissaData";
        public string YTitle { get; private set; } = "OrdinateData";
        public ScatterChartcs()
        {
            InitializeComponent();
            plotSurface = new NPlot.Windows.PlotSurface2D
            {
                Dock = DockStyle.Fill,
                BackColor = SystemColors.Control
            };
            Controls.Add(plotSurface);
            plotSurface.RightMenu = NPlot.Windows.PlotSurface2D.DefaultContextMenu;
            ChartTitle = "ScatterChart";
            XTitle = "AbscissaData";
            YTitle = "OrdinateData";
            preData = new PreprocessData();
        }
        /// <summary>
        /// 离散点
        /// </summary>
        /// <param name="dt">列依次为float数据、string等级、stringID的数据源</param>
        /// <param name="title">图表标题</param>
        /// <param name="xtitle">x轴坐标</param>
        /// <param name="ytitile">y轴坐标</param>
        /// <param name="standards">从低到高的标准值列表</param>
        /// <param name="Levels">按从低到高的级别列表</param>
        public void PlotParticles(DataTable data, string title, string xtitle, string ytitile, List<float> standards, List<string> Levels)
        {
            var dt = preData.PreprocessDataTabel(data, Levels);
            plotSurface.Clear();
            plotSurface.SetDataTable(dt);
            plotSurface.Title = title;
            plotSurface.TitleFont = new Font("微软雅黑", 12, FontStyle.Bold);
            XTitle = dt.Columns[0].ColumnName;//index
            YTitle = dt.Columns[1].ColumnName;//value
            var name = dt.Columns[2].ColumnName;//level
            for (int i = 0; i < standards.Count(); i++)
                plotSurface.Add(new HorizontalLine(standards[i], GetColor(i)));
            PointPlot pp = new PointPlot();
            pp.DataSource = dt;
            pp.AbscissaData = XTitle;
            pp.OrdinateData = YTitle;

            pp.Marker = new Marker(Marker.MarkerType.FilledCircle, 4, new Pen(Color.Blue));
            plotSurface.Add(pp, PlotSurface2D.XAxisPosition.Bottom, PlotSurface2D.YAxisPosition.Left);
            plotSurface.XAxis1.TickTextNextToAxis = false;
            plotSurface.XAxis1.TicksAngle = (float)Math.PI / 2.0f;
            LabelAxis la = new LabelAxis(plotSurface.XAxis1);
            foreach (var level in preData.DitLevel)
            {
                la.AddLabel(level.Key, level.Value);
            }

            la.TicksBetweenText = true;
            la.Label = xtitle;
            plotSurface.XAxis1 = la;
            //set axes
            LinearAxis ly = (LinearAxis)plotSurface.YAxis1;
            ly.Label = ytitile;
            ly.NumberOfSmallTicks = 2;
            plotSurface.Refresh();
        }
        private Color GetColor(int index)
        {
            if (index >= StandardColor.Length)
                index = StandardColor.Length % index;
            return StandardColor[index];
        }
    }
}
